<template>
  <div>
    <div class="head_fixed">
      <div class="nav_bar">
        <div><span v-for="item in allColumn" :class="currColumn==item.id?'nav_select':''"
                   @click="currColumn=item.id">{{item.name}}</span></div>
        <div></div>
      </div>
      <div class="column" v-show="isSecond">
        <div><span v-for="item in second" :class="currSecond==item.id?'col_select':''"
                   @click="currSecond=item.id">{{item.name}}</span></div>
      </div>
    </div>
    <div :class="isSecond?'infoList':'infoList1'" v-infinite-scroll="loadMore" infinite-scroll-disabled="loading"
         infinite-scroll-distance="10">
      <div class="module" v-for="(item,index) in dataList" @click="toNwesDetail(item.postId,item.outLink)">
        <div class="left">
          <div class="title"><i v-show="item.isTop==1"></i>{{item.title}}</div>
          <div class="source">{{item.source}}</div>
        </div>
        <div class="right" v-show="item.imageUrl"><img :src="item.imageUrl"/></div>
      </div>
    </div>
    <div class="no_more">没有更多了~</div>
    <div class="pop_sign" v-show="pop_sign">
      <div class="pop_title">资讯订阅</div>
      <div class="pop_content">
        <span v-for="(item,index) in allColumn" :class="item.select?'pop_select':''"
              @click="chooseMy(index)">{{item.name}}</span></div>
      <div class="pop_btn">
        <div @click="pop_sign=false">取消</div>
        <div @click="btnSure">确认</div>
      </div>
    </div>
  </div>
</template>
<script>
  import Vue from "vue"
  export default {
    name: 'index',
    data () {
      return {
        pop_sign: false,
        allColumn: [],
        selectColumn: [{"value": "1001", "name": "新闻资讯"}, {"value": "1003", "name": "人事人才"}],
        currColumn: '',
        second: [],
        currSecond: '',
        dataList: [],
        isSecond: true,
        pageIndex: 1,
        loading: true,
        currentId: ''
      }
    },
    methods: {
      loadAllColumn(){
        this.$http.get("/site/category/list", {params: {parentId: 9999}}).then(res => {
          this.allColumn = res.body;
          this.currColumn = res.body[0].id;
          this.getSecond(this.currColumn);
        })
      },
      addColumn(){
        let i = 0;
        for (i; i < this.allColumn.length; i++) {
          this.allColumn[i].select = false;
          for (let j = 0; j < this.selectColumn.length; j++) {
            if (this.selectColumn[j].value == this.allColumn[i].id) {
              this.allColumn[i].select = true;
            }
          }
        }
        this.pop_sign = true;
      },
      chooseMy(index){
        this.allColumn[index].select = !this.allColumn[index].select;
        Vue.set(this.allColumn, index, this.allColumn[index]);
      },
      btnSure(){
        this.selectColumn = [];
        let i = 0;
        for (i; i < this.allColumn.length; i++) {
          if (this.allColumn[i].select) {
            this.selectColumn.push({value: this.allColumn[i].id, name: this.allColumn[i].name});
          }
        }
        this.currColumn = this.selectColumn[0].value;
        this.getSecond(this.currColumn);
        this.pop_sign = false;
      },
      getSecond(num){
        this.$http.get("/site/category/list", {params: {parentId: num}}).then(res => {
          this.second = res.body;
          if (res.body.length > 0) {
            this.currSecond = this.second[0].id;
            this.getList(this.currSecond);
            this.isSecond = true;
          } else {
            this.getList(num);
            this.isSecond = false;
          }
        })
      },
      getList(id){
        this.currentId = id;
        scrollTo(0, 0);
        this.pageIndex = 1;
        this.$http.get("/app/news/list", {
          params: {
            categoryId: id,

            page_index: this.pageIndex,
            page_size: 10
          }
        }).then(res => {
          console.log(res);
          this.dataList = res.body;
          if (res.body.length < 10) {
            this.loading = true;
          } else {
            this.loading = false;
          }
        })
      },
      loadMore(){
        this.pageIndex++;
        this.$http.get("/app/news/list", {
          params: {
            categoryId: this.currentId,
            page_index: this.pageIndex,
            page_size: 10
          }
        }).then(res => {
          console.log(res);
          this.dataList.push.apply(this.dataList, res.body);
          if (res.body.length < 10) {
            this.loading = true;
          } else {
            this.loading = false;
          }
        })
      },
      toNwesDetail(id,ifout){
          if (ifout) {
            if (!this.util.judgePhnoe("isWeixin")) {
              if (this.util.judgePhnoe("isiOS")) {
                window.webkit.messageHandlers.newHtml.postMessage(ifout);
              } else if (this.util.judgePhnoe("isAndroid")) {
                window.AndroidInterface.newHtml(ifout);
              } else {
                window.location.href =ifout;
              }
            } else {
              window.location.href = ifout;
            }
          } else {
            let url = window.location.href;
            let resultUrl = url.substr(0, url.indexOf('#'))+ '#/newsDetail?postId=' + id;
            if (!this.util.judgePhnoe("isWeixin")) {
              if (this.util.judgePhnoe("isiOS")) {
                window.webkit.messageHandlers.newHtml.postMessage(resultUrl+"&source=1");
              } else if (this.util.judgePhnoe("isAndroid")) {
                window.AndroidInterface.newHtml(resultUrl+"&source=1");
              } else {
                window.location.href = resultUrl+"&source=0";
              }
            } else {
              window.location.href = resultUrl+"&source=3";
            }
          }
      }
    },
    created() {
      this.loadAllColumn();
//      this.getSecond(this.currColumn);
    },
    watch: {
      currColumn(){
        this.getSecond(this.currColumn);
      },
      currSecond(){
        this.getList(this.currSecond);
      }
    }
  }
  document.body.style.background="#f2f2f2";
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .head_fixed {
    position: fixed;
    width: 100%;
    top: 0;
    left: 0px;
    background: #F3F3F3;
    z-index: 1;
  }

  .nav_bar {
    height: 45px;
    line-height: 45px;
    background-color: #ffffff;
    font-size: 14px;
    width: 100%;
    overflow: hidden;
  }

  .nav_bar > div {
    float: left;
  }

  .nav_bar > div:first-child {
    height: 62px;
    width: 96%;
    overflow-x: scroll;
    overflow-y: hidden;
    white-space: nowrap;
  }

  .nav_bar > div:last-child {
    width: 4%;
    /*background: url("../../assets/images/icon/icon-add.png") no-repeat center;*/
    /*background-size: 16px;*/
    height: 100%;
  }

  .nav_bar > div > span {
    display: inline-block;
    margin-left: 15px;
    margin-right: 5px;
    height: 44px;
    font-size: 16px;
  }

  .nav_select {
    color: #3399FF;
    border-bottom: 1px solid #3399FF;
    font-size: 16px;
  }

  .column {
    height: 45px;
    font-size: 14px;
    overflow: hidden;
  }

  .column > div {
    height: 62px;
    width: 100%;
    overflow-x: scroll;
    overflow-y: hidden;
    white-space: nowrap;
  }

  .column span {
    display: inline-block;
    margin-left: 15px;
    margin-right: 5px;
    height: 25px;
    border: 1px solid #979797;
    border-radius: 50px;
    padding: 0 10px;
    line-height: 25px;
    margin-top: 10px;
  }

  .col_select {
    color: #FFFFFF;
    background: #3399FF;
    border: 1px solid #3399FF !important;
  }

  .infoList {
    background: #ffffff;
    margin-top: 100px;
  }

  .infoList1 {
    background: #ffffff;
    margin-top: 55px;
  }

  .infoList > div:not(:last-child), .infoList1 > div:not(:last-child) {
    border-bottom: 1px solid #DFDFDF;
  }

  .module {
    display: flex;
    padding: 10px 15px 10px 0px;
    max-height: 103px;
    margin-left: 4%;
  }

  .module img {
    width: 100%;
    height: 100%;
  }

  /*.module:not(:last-child):after{border-bottom: 1px solid #DFDFDF;left: 0px;top:0px;width: 99.5%;position: relative; display: block;content: "";transform: scaleY(0.5);}*/

  .left {
    flex: 1;
  }

  .right {
    margin-left: 15px;
    width: 110px;
    height: 71px;
  }

  .title {
    font-size: 16px;
    -o-text-overflow: ellipsis;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    width: 100%;
  }

  .source {
    font-size: 12px;
    color: #888;
    margin-top: 10px;
  }

  .title > i {
    position: relative;
    margin: 5px 10px 2px 0;
    vertical-align: top;
    display: inline-block;
    width: 28px;
    height: 15px;
    background: url("../../assets/images/icon/icon-topline.png") no-repeat;
    background-size: 100% 100%;
  }

  .pop_sign {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.60);
    z-index: 2;
  }

  .pop_title {
    background: #FFFFFF;
    line-height: 45px;
    font-size: 14px;
    color: #333333;
    text-align: center;
  }

  .pop_content {
    background: #F8F8F8;
    font-size: 14px;
    padding-top: 19px;
  }

  .pop_content > span {
    display: inline-block;
    box-sizing: border-box;
    width: 28%;
    background: #FFFFFF;
    color: #333333;
    border: 1px solid #DFDFDF;
    border-radius: 4px;
    line-height: 40px;
    text-align: center;
    margin-left: 4%;
    margin-bottom: 15px;
  }

  .pop_select {
    border: #38ADFF !important;
    color: #FFFFFF !important;
    background: #38ADFF !important;
  }

  .pop_btn {
    overflow: hidden;
  }

  .pop_btn > div {
    float: left;
    width: 50%;
    text-align: center;
    font-size: 18px;
    line-height: 45px;
    box-sizing: border-box;
  }

  .pop_btn > div:first-child {
    background: #ffffff;
    border-top: 1px solid #979797;
  }

  .pop_btn > div:last-child {
    background: #4990E2;
    border-top: 1px solid #4990E2;
    color: #FFFFFF;
  }

  .no_more {
    font-size: 13px;
    color: #8C8C8C;
    text-align: center;
    margin: 15px 0px 20px 0px;
  }
</style>
